package com.bcst.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/*
 *@description 欢迎关注公众号 编程识堂,每日更新技术相关文章，关注我不迷路
 *@author 小堂
 *@create 2023/9/2 16:11
 * 事务管理器类：负责手动事务的开启、提交、回滚
 */
@Service
public class TransactionManager {

   @Autowired
   private ConnectionUtils connectionUtils;
    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        //ConnectionUtils.getInstance().getCurrentThreadConn().setAutoCommit(false);
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }


    // 提交事务
    public void commit() throws SQLException {
        System.out.println("事务提交了。。。。。");
        //ConnectionUtils.getInstance().getCurrentThreadConn().commit();
        connectionUtils.getCurrentThreadConn().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        System.out.println("事务回滚了。。。。。");
        //ConnectionUtils.getInstance().getCurrentThreadConn().rollback();
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
